#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef WIN32
#include <curses.h>
#else
#include <ncurses.h>
#endif

#include "Jeu.h"

void NcursesAff (WINDOW* win, const Jeu* j)
{
	wclear(win);
	int x,y;
	int niveau, score;
	const Terrain* ter=GetTerrain(j);
	niveau=GetNiveau(j);
	score=GetScore(j);
	for(x=0;x<HAUTEUR_MAX;x++)
	{
		for(y=LARGEUR_MIN;y<LARGEUR_MAX;y++)
		{
			mvwprintw(win, x, y, "%d", GetEtatTerrain(ter,x,y));
		}
	}
	/* affichage piece a rajouter */
	mvwprintw(win,15, 15, "niveau: %d", niveau);
	mvwprintw(win,16, 15, "score: %d", score);
}

void NcursesBoucle(Jeu* j)
{
	WINDOW* win;
	int c, ok;
	initscr();
	clear();
	noecho();
	cbreak();
	win=newwin(HAUTEUR_TOTALE+30, LARGEUR_TOTALE+30, 2, 2);
	keypad(win, 1);
	nodelay(win, TRUE);
	ok=1;

	do
	{
		PieceTomber(j);	
		c= wgetch(win);
		switch(c)
		{
			case KEY_LEFT:
				ActionClavier( j, 'g');
				break;
			case KEY_RIGHT:
				ActionClavier( j, 'd');
				break;
			case KEY_UP:
				ActionClavier( j, 'h');
				break;
			case KEY_DOWN:
				ActionClavier( j, 'b');
				break;
		}
		ok=j->fin;
		if(ok)
		{
			NcursesAff(win, j);
			sleep(1/j->niveau);
		}
	}
	
	while(ok);
	endwin();
}
